﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AppManagement.Repositories;
using AppManagement.Helpers;
using AppManagement.Models;

namespace AppManagement.Reports
{
    public partial class FrmInThongKeTonKho : Form
    {
        public string Qui { get; set; }
        public int Nam { get; set; }

        public FrmInThongKeTonKho()
        {
            InitializeComponent();
        }

        public void LoadData()
        {
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("ID");
            dt.Columns.Add("TEN_THIET_BI");
            dt.Columns.Add("TONDAUKI");
            dt.Columns.Add("NHAP");
            dt.Columns.Add("XUAT");
            dt.Columns.Add("TONCUOIKI");

            var lst = THIETBIRepository.SelectAll();

            for (int i = 0; i < lst.Count; i++)
            {
                var slTonDauKy = LoadSoLuongTonDauKi(lst[i].THIET_BI_ID);
                var slNhap = LoadSoLuongNhap(lst[i].THIET_BI_ID);
                var slXuat = LoadSoLuongXuat(lst[i].THIET_BI_ID);
                dt.Rows.Add((i + 1).ToString(), lst[i].THIET_BI_ID, lst[i].TEN_THIET_BI, slTonDauKy, slNhap, slXuat, slTonDauKy + slNhap - slXuat);
            }

            rptTonKho rpt = new rptTonKho();
            rpt.DataDefinition.FormulaFields["NgayLapBaoCao"].Text = "'" + String.Format("{0:dd/MM/yyyy}", DateTime.Now) + "'";
            rpt.DataDefinition.FormulaFields["NguoiLap"].Text = "'" + GlobalVariables.g_CurrentUser.TEN_NGUOI_DUNG + "'";
            rpt.DataDefinition.FormulaFields["Qui"].Text = "'" + this.Qui.Substring(this.Qui.Length - 1) + "'";
            rpt.DataDefinition.FormulaFields["Nam"].Text = "'" + this.Nam + "'";

            rpt.SetDataSource(dt);
            crystalReportViewer1.ReportSource = rpt;
        }

        private int LoadSoLuongNhap(int idthietbi)
        {
            var lstPhieu = PHIEUNHAPKHORepository.SelectAll();
            var num = 0;
            foreach (var phieu in lstPhieu)
            {
                switch (this.Qui)
                {
                    case "Quí 1":
                        if (phieu.NGAY_NHAP_KHO.Value.Year == this.Nam && 1 <= phieu.NGAY_NHAP_KHO.Value.Month && phieu.NGAY_NHAP_KHO.Value.Month <= 3)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUNHAPKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 2":
                        if (phieu.NGAY_NHAP_KHO.Value.Year == this.Nam && 4 <= phieu.NGAY_NHAP_KHO.Value.Month && phieu.NGAY_NHAP_KHO.Value.Month <= 6)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUNHAPKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 3":
                        if (phieu.NGAY_NHAP_KHO.Value.Year == this.Nam && 7 <= phieu.NGAY_NHAP_KHO.Value.Month && phieu.NGAY_NHAP_KHO.Value.Month <= 9)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUNHAPKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 4":
                        if (phieu.NGAY_NHAP_KHO.Value.Year == this.Nam && 10 <= phieu.NGAY_NHAP_KHO.Value.Month && phieu.NGAY_NHAP_KHO.Value.Month <= 12)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUNHAPKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                }
            }
            return num;
        }

        private int LoadSoLuongXuat(int idthietbi)
        {
            var lstPhieu = PHIEUXUATKHORepository.SelectAll();
            var num = 0;
            foreach (var phieu in lstPhieu)
            {
                switch (this.Qui)
                {
                    case "Quí 1":
                        if (phieu.NGAY_XUAT_KHO.Value.Year == this.Nam && 1 <= phieu.NGAY_XUAT_KHO.Value.Month && phieu.NGAY_XUAT_KHO.Value.Month <= 3)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUXUATKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 2":
                        if (phieu.NGAY_XUAT_KHO.Value.Year == this.Nam && 4 <= phieu.NGAY_XUAT_KHO.Value.Month && phieu.NGAY_XUAT_KHO.Value.Month <= 6)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUXUATKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 3":
                        if (phieu.NGAY_XUAT_KHO.Value.Year == this.Nam && 7 <= phieu.NGAY_XUAT_KHO.Value.Month && phieu.NGAY_XUAT_KHO.Value.Month <= 9)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUXUATKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                    case "Quí 4":
                        if (phieu.NGAY_XUAT_KHO.Value.Year == this.Nam && 10 <= phieu.NGAY_XUAT_KHO.Value.Month && phieu.NGAY_XUAT_KHO.Value.Month <= 12)
                        {
                            var lstChiTiet = phieu.CHITIETPHIEUXUATKHO.Where(n => n.THIET_BI_ID == idthietbi).ToList();
                            foreach (var thietbi in lstChiTiet)
                            {
                                num += thietbi.SO_LUONG.Value;
                            }
                        }
                        break;
                }
            }
            return num;
        }

        private int LoadSoLuongTonDauKi(int idthietbi)
        {
            var num = 0;
            TONKHODAUKY tondauky = null;

            switch (this.Qui)
            {
                case "Quí 1":
                    tondauky = TONKHODAUKYRepository.SelectByThietBiQuiNam(idthietbi, 4, Nam - 1);
                    break;
                case "Quí 2":
                    tondauky = TONKHODAUKYRepository.SelectByThietBiQuiNam(idthietbi, 1, Nam);
                    break;
                case "Quí 3":
                    tondauky = TONKHODAUKYRepository.SelectByThietBiQuiNam(idthietbi, 2, Nam);
                    break;
                case "Quí 4":
                    tondauky = TONKHODAUKYRepository.SelectByThietBiQuiNam(idthietbi, 3, Nam);
                    break;
            }
            if (tondauky != null)
            {
                num = tondauky.SoLuongTon.Value;
            }
            return num;
        }

        private void FrmInPhieuDatHang_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
